#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_ReadDTCInfomation_0x19_01_test.py
@Time    :   2025/04/03 13:31:28
@Author  :   YangLiang
@Version :   1.0
@Contact :   yangl@genvict.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from commDriver.modbus_plc import *
from gettext import find
from xmlrpc.client import Fault
from pytest_assume.plugin import assume

import pytest
import logging
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false

sys.dont_write_bytecode = True


reportNumberOfDTCByStatusMask_noFault = [
   ('19 01 01', '59 01 09 00 00'),
   ('19 01 08', '59 01 09 00 00'),
   ('19 01 09', '59 01 09 00 00'),
   ('19 01 FF', '59 01 09 00 00'),
]

class TestFault_def:
   def TestFault(self, Driver, iStep, faultCount=3):
      # 设置电压为17V,并等待1.5s 制造 Overvoltage 故障,读取当前故障数量
      iStep = Driver['CAN'].SetVolt(Driver, 17, 7, iStep)
      faultCount += 1 
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 01 01', [0x59, 0x01, 0x09, 0x00, 0x00, faultCount], 0, iStep, False, 'UDS', 'noClearFault')

      # 设置电压为12V,并等待1.5s 恢复 Overvoltage 故障,读取当前故障数量
      iStep = Driver['CAN'].SetVolt(Driver, 12, 3, iStep)
      faultCount -= 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 01 01', [0x59, 0x01, 0x09, 0x00, 0x00, faultCount], 0, iStep, False, 'UDS', 'noClearFault')

      # 设置电压为8.5V,并等待1.5s 制造 Undervoltage 故障,读取当前故障数量
      iStep = Driver['CAN'].SetVolt(Driver, 8.5, 7, iStep)
      faultCount += 1 
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 01 01', [0x59, 0x01, 0x09, 0x00, 0x00, faultCount], 0, iStep, False, 'UDS', 'noClearFault')

      # 设置电压为12V,并等待1.5s 恢复 Undervoltage 故障,读取当前故障数量
      iStep = Driver['CAN'].SetVolt(Driver, 12, 3, iStep)
      faultCount -= 1 
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 01 01', [0x59, 0x01, 0x09, 0x00, 0x00, faultCount], 0, iStep, False, 'UDS', 'noClearFault')

      # '''制造 DLostCommunication 错误,故障数量需要累加'''
      # iStep = Driver['CAN'].SetLostCommunicationStatus(Driver, iStep, False)
      # faultCount += 1 
      # time.sleep(0.5)
      # resp_str = f"59 02 09 00 00 {faultCount:02X}"
      # iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 01 01', resp_str, 0, iStep, False, 'UDS', 'noClearFault')
      # iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02', 0, iStep, False, 'UDS', 'noClearFault')

      # '''恢复 DLostCommunication 错误,故障数量需要减少'''
      # iStep = Driver['CAN'].SetLostCommunicationStatus(Driver, iStep, True)
      # faultCount -= 1 
      # time.sleep(0.5)
      # resp_str = f"59 02 09 00 00 {faultCount:02X}"
      # iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 01 01', resp_str, 0, iStep, False, 'UDS', 'noClearFault')
      # iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02', 0, iStep, False, 'UDS', 'noClearFault')
      # '''清除所有故障'''
      # iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '14 FF FF FF', '54', 0, iStep, False, 'UDS', 'noClearFault')
      return iStep,faultCount


class Test_APP_reportNumberOfDTCByStatusMask_phy(TestFault_def,object):
   '''NO Fault'''
   ids = [f"Request {req}" for req, resp in reportNumberOfDTCByStatusMask_noFault]
   @allure.severity('normal')
   @allure.title('物理寻址-默认会话-不产生DTC状态下DTC数量读取')
   @allure.description('验证物理寻址默认会话下通过物理寻址读取符合状态掩码的DTC数量(0x19 01)')
   @pytest.mark.parametrize('req,resp', reportNumberOfDTCByStatusMask_noFault, ids=ids)
   def test_reportNumberOfDTCByStatusMask_No_Fault_Default_Phy(self, action, Driver, req, resp):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDS', 'ClearFault')
      with allure.step(f'Step{iStep}: check response length # size = 6'):
         with assume: assert size == 6
         iStep += 1
      with allure.step(f'Step{iStep}: check falut dtc number  # number <= 2'):
         with assume: assert uds_resp[5] <= 2
         iStep += 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02 09', 0, iStep, False, 'UDS', 'noClearFault')


   ids = [f"Request {req}" for req, resp in reportNumberOfDTCByStatusMask_noFault]
   @allure.severity('normal')
   @allure.title('物理寻址-扩展会话-不产生DTC状态下DTC数量读取')
   @allure.description('验证物理寻址扩展会话下通过物理寻址读取符合状态掩码的DTC数量(0x19 01)')
   @pytest.mark.parametrize('req,resp', reportNumberOfDTCByStatusMask_noFault, ids=ids)
   def test_reportNumberOfDTCByStatusMask_No_Fault_Extended_Phy(self, action,  Driver, req, resp):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDS', 'ClearFault')
      with allure.step(f'Step{iStep}: check response length # size = 6'):
         with assume: assert size == 6
         iStep += 1
      with allure.step(f'Step{iStep}: check falut dtc number  # number <= 2'):
         with assume: assert uds_resp[5] <= 2
         iStep += 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 09', 0, iStep, False, 'UDS', 'noClearFault')

   @allure.severity('normal')
   @allure.title('物理寻址-默认会话-产生DTC状态下DTC数量变化')
   @allure.description('验证物理寻址默认会话下通过物理寻址读取符合状态掩码的DTC数量(0x19 01)')
   def test_reportNumberOfDTCByStatusMask_Have_Fault_Default_Phy(self, action,  Driver):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 01 01','59 01 09', 1, iStep, False, 'UDS', 'ClearFault')
      with allure.step(f'Step{iStep}: check response length # size = 6'):
         with assume: assert size == 6
         iStep += 1
      with allure.step(f'Step{iStep}: check falut dtc number  # number <= 2'):
         with assume: assert uds_resp[5] <= 2
         iStep += 1
         faultCount = uds_resp[5]
      super().TestFault(Driver, iStep,faultCount)

   @allure.severity('normal')
   @allure.title('物理寻址-扩展会话-产生DTC状态下DTC数量变化')
   @allure.description('验证物理寻址扩展会话下通过物理寻址读取符合状态掩码的DTC数量(0x19 01)')
   def test_reportNumberOfDTCByStatusMask_Have_Fault_Extended_Phy(self, action,  Driver):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 01 01','59 01 09', 3, iStep, False, 'UDS', 'ClearFault')
      with allure.step(f'Step{iStep}: check response length # size = 6'):
         with assume: assert size == 6
         iStep += 1
      with allure.step(f'Step{iStep}: check falut dtc number  # number <= 2'):
         with assume: assert uds_resp[5] <= 2
         iStep += 1
         faultCount = uds_resp[5]
      super().TestFault(Driver, iStep,faultCount)

class Test_APP_reportNumberOfDTCByStatusMask_Fun(TestFault_def, object):
   '''NO Fault'''
   ids = [f"Request {req}" for req, resp in reportNumberOfDTCByStatusMask_noFault]
   @allure.severity('normal')
   @allure.title('功能寻址-默认会话-不产生DTC状态下DTC数量读取')
   @allure.description('验证功能寻址默认会话下通过功能寻址读取符合状态掩码的DTC数量(0x19 01)')
   @pytest.mark.parametrize('req,resp', reportNumberOfDTCByStatusMask_noFault, ids=ids)
   def test_reportNumberOfDTCByStatusMask_No_Fault_Default_Func(self, action,  Driver, req, resp):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun', 'ClearFault')
      with allure.step(f'Step{iStep}: check response length # size = 6'):
         with assume: assert size == 6
         iStep += 1
      with allure.step(f'Step{iStep}: check falut dtc number  # number <= 2'):
         with assume: assert uds_resp[5] <= 2
         iStep += 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 09', 0, iStep, False, 'UDS', 'noClearFault')

   ids = [f"Request {req}" for req, resp in reportNumberOfDTCByStatusMask_noFault]
   @allure.severity('normal')
   @allure.title('功能寻址-扩展会话-不产生DTC状态下DTC数量读取')
   @allure.description('验证功能寻址扩展会话下通过功能寻址读取符合状态掩码的DTC数量(0x19 01)')
   @pytest.mark.parametrize('req,resp', reportNumberOfDTCByStatusMask_noFault, ids=ids)
   def test_reportNumberOfDTCByStatusMask_No_Fault_Extended_Func(self, action,  Driver, req, resp):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun', 'ClearFault')
      with allure.step(f'Step{iStep}: check response length # size = 6'):
         with assume: assert size == 6
         iStep += 1
      with allure.step(f'Step{iStep}: check falut dtc number  # number <= 2'):
         with assume: assert uds_resp[5] <= 3
         iStep += 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 01', '59 02 09', 0, iStep, False, 'UDS', 'noClearFault')

   @allure.severity('normal')
   @allure.title('功能寻址-默认会话-产生DTC状态下DTC数量变化')
   @allure.description('验证功能寻址默认会话下通过功能寻址读取符合状态掩码的DTC数量(0x19 01)')
   def test_reportNumberOfDTCByStatusMask_Have_Fault_Default_Func(self, action,  Driver):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 01 01','59 01 09', 1, iStep, False, 'UDSFun', 'ClearFault')
      with allure.step(f'Step{iStep}: check response length # size = 6'):
         with assume: assert size == 6
         iStep += 1
      with allure.step(f'Step{iStep}: check falut dtc number  # number <= 2'):
         with assume: assert uds_resp[5] <= 2
         iStep += 1
         faultCount = uds_resp[5]
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 02 09', '59 02', 0, iStep, False, 'UDSFun', 'noClearFault')
      
      super().TestFault(Driver, iStep,faultCount)

   @allure.severity('normal')
   @allure.title('功能寻址-扩展会话-产生DTC状态下DTC数量变化')
   @allure.description('验证功能寻址扩展会话下通过功能寻址读取符合状态掩码的DTC数量(0x19 01)')
   def test_reportNumberOfDTCByStatusMask_Have_Fault_Extended_Fun(self, action,  Driver):
      iStep = 1 
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '19 01 01','59 01 09', 3, iStep, False, 'UDSFun', 'ClearFault')
      with allure.step(f'Step{iStep}: check response length # size = 6'):
         with assume: assert size == 6
         iStep += 1
      with allure.step(f'Step{iStep}: check falut dtc number  # number <= 2'):
         with assume: assert uds_resp[5] <= 2
         iStep += 1
         faultCount = uds_resp[5]
      super().TestFault(Driver, iStep,faultCount)
